package com.jcfk.eam.service.base;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.base.EamFaultPhenomenonDao;
import com.jcfk.eam.domain.dto.base.EamFaultModeAssetDTO;
import com.jcfk.eam.domain.dto.base.EamFaultPhenomenonDTO;
import com.jcfk.eam.domain.po.base.EamFaultModeAsset;
import com.jcfk.eam.domain.po.base.EamFaultPhenomenon;
import com.jcfk.eam.domain.query.base.EamFaultPhenomenonQuery;
import com.jcfk.eam.domain.uploadExcel.base.EamFaultPhenomenonUploadExcel;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 故障现象
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-10-19
 */
@Service
public class EamFaultPhenomenonService extends BaseService<EamFaultPhenomenonDao, EamFaultPhenomenon> {

    /**
     * EamFaultPhenomenon分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamFaultPhenomenonDTO> queryByPage(DataPage<EamFaultPhenomenonDTO> page, EamFaultPhenomenonQuery query) {
        // 分页查询
        query.setFaultPhenomenon(StringUtils.isEmpty(query.getFaultPhenomenon()) ? query.getFaultPhenomenon() : query.getFaultPhenomenon() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamFaultPhenomenon是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamFaultPhenomenonDTO dto) {
        QueryWrapper<EamFaultPhenomenon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入故障现象
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamFaultPhenomenonDTO dto) {

        // 保存
        EamFaultPhenomenon item = new EamFaultPhenomenon();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);
        item.setEnabled(AvailableStatus.ENABLE.getValue());

        super.save(item);
    }

    /**
     * 更新故障现象
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamFaultPhenomenonDTO dto) {

        // 保存
        EamFaultPhenomenon item = new EamFaultPhenomenon();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除故障现象(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamFaultPhenomenonDTO dto) {

        EamFaultPhenomenon item = new EamFaultPhenomenon();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamFaultPhenomenon> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamFaultPhenomenon item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取故障现象信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamFaultPhenomenonDTO get(String id) {
        // 查询数据
        EamFaultPhenomenon item = this.baseMapper.selectById(id);
        EamFaultPhenomenonDTO dto = new EamFaultPhenomenonDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 插入故障现象
     *
     * @param list 批量插入故障现象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<EamFaultPhenomenonUploadExcel> list) {

        List<EamFaultPhenomenon> savelist = new ArrayList<>();
        list.forEach(itemOne -> {
            // 保存
            EamFaultPhenomenon item = new EamFaultPhenomenon();
            BeanUtils.copyProperties(itemOne, item);
            this.setDefaultValue(item);
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBigBatch(savelist);
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamFaultPhenomenonDTO>
     */
    public List<EamFaultPhenomenonDTO> getList(EamFaultPhenomenonQuery query) {
        // 查询条件
        QueryWrapper<EamFaultPhenomenon> qw = new QueryWrapper<>();
        qw.lambda().eq(query.getAssetKind()!=null, EamFaultPhenomenon::getAssetKind, query.getAssetKind());
        qw.lambda().eq(!StrUtil.isEmpty(query.getFaultModeId()), EamFaultPhenomenon::getFaultModeId, query.getFaultModeId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getAssetTypeId()), EamFaultPhenomenon::getAssetTypeId, query.getAssetTypeId());
        qw.lambda().like(!StrUtil.isEmpty(query.getFaultPhenomenon()), EamFaultPhenomenon::getFaultPhenomenon, query.getFaultPhenomenon());
        qw.lambda().eq( EamFaultPhenomenon::getEnabled, AvailableStatus.ENABLE.getValue());
        qw.lambda().eq( EamFaultPhenomenon::getDeleted, DeleteFlag.NOTDELETED.getValue());
        // 查询数据
        List<EamFaultPhenomenon> list = super.list(qw);

        return convertList(list, EamFaultPhenomenonDTO.class);
    }

    /**
     * 根据主键获取故障现象信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamFaultPhenomenon item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

